﻿using System;
using System.IO;
using System.Windows.Forms;
using System.Net;
using System.Collections.Generic;

namespace Chromium_Updater
{
    class Settings
    {
        private static IniFile ini;

        #region Settings manipulation (load/update/reset)

        public static void load()
        {
            string path = Application.StartupPath + "\\settings.ini";
            if (!File.Exists(path)) { File.Create(path); }
            ini = new IniFile(path);
            //
            update(); // 1.3.7 -> 1.4
        }

        private static void update()
        {
            string[] keys = ini.GetKeyNames("Settings");
            foreach (string key in keys)
            {
                // [Settings]Updater URLs -> [Updater URLs]
                if (key == "Latest_Revision_URL")
                {
                    ini.WriteValue("CUU", "Latest_Revision", ini.GetString("Settings", "Latest_Revision_URL", null));
                    ini.DeleteKey("Settings", "Latest_Revision_URL");
                }
                if (key == "Specific_Revision_URL")
                {
                    ini.WriteValue("CUU", "Specific_Revision", ini.GetString("Settings", "Specific_Revision_URL", null));
                    ini.DeleteKey("Settings", "Specific_Revision_URL");
                }
                // Update Frequency -> Check Interval
                if (key == "Update_Frequency")
                {
                    ini.WriteValue("Settings", "Check_Interval", ini.GetString("Settings", "Update_Frequency", null));
                    ini.DeleteKey("Settings", "Update_Frequency");
                }
            }
        }

        public static void reset()
        {
            Latest_Revision_URL   = "http://commondatastorage.googleapis.com/chromium-browser-continuous/Win/LAST_CHANGE";
            Specific_Revision_URL = "http://commondatastorage.googleapis.com/chromium-browser-continuous/Win/";
            Start_Hidden          = false;
            Check_Interval        = 60;
            SmartSense            = false;
            Installer_Timeout     = 120;
            Download_Directory    = "%CURRENT_DIRECTORY%/Downloads";
            Auto_Download         = false;
            Show_Updater_Changes  = true;
            CUpdater_Log_Path     = "%CURRENT_DIRECTORY%/CUpdater.log";
        }

        #endregion

        #region Chromium Updater URLs

        private static string lru;
        private static string sru;

        public static string Latest_Revision_URL
        {
            get 
            {
                if (String.IsNullOrEmpty(lru))
                {
                    lru = ini.GetString("CUU", "Latest_Revision", "http://commondatastorage.googleapis.com/chromium-browser-continuous/Win/LAST_CHANGE");
                }
                return lru;
            }
            set
            {
                lru = value;
                ini.WriteValue("CUU", "Latest_Revision", value);
            }
        }

        public static string Specific_Revision_URL
        {
            get 
            {
                if (String.IsNullOrEmpty(sru))
                {
                    sru = ini.GetString("CUU", "Specific_Revision", "http://commondatastorage.googleapis.com/chromium-browser-continuous/Win/");
                }
                return sru;
            }
            set
            {
                sru = value;
                ini.WriteValue("CUU", "Specific_Revision", value);
            }
        }

        public static bool validURL(string url)
        {
            if (Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute))
            {
                return true;
            }
            return false;
        }

        #endregion

        #region Start Hidden

        private static int sh  = -1;

        public static bool Start_Hidden
        {
            get
            {
                if (sh < 0) { sh = ini.GetInt32("Settings", "Start_Hidden", 0); }
                return Convert.ToBoolean(sh);
            }
            set
            {
                sh = Convert.ToInt32(value);
                ini.WriteValue("Settings", "Start_Hidden", Convert.ToInt32(value));
            }
        }

        #endregion

        #region Check Interval

        private static int uf = -1;

        public static int Check_Interval
        {
            get
            {
                if (uf < 0) { uf = ini.GetInt32("Settings", "Check_Interval", 60); }
                return uf;
            }
            set
            {
                uf = value;
                ini.WriteValue("Settings", "Check_Interval", value);
            }
        }

        #endregion

        #region SmartSense

        private static int ss = -1;

        public static bool SmartSense
        {
            get
            {
                if (ss < 0) { sh = ini.GetInt32("Settings", "SmartSense", 0); }
                return Convert.ToBoolean(ss);
            }
            set
            {
                ss = Convert.ToInt32(value);
                ini.WriteValue("Settings", "SmartSense", Convert.ToInt32(value));
            }
        }

        #endregion

        #region Current Revision

        private static int cr = -1;

        public static int Current_Revision
        {
            get
            {
                if (cr == -1) { cr = ini.GetInt32("Settings", "Current_Revision", 0); }
                return cr;
            }
            set
            {
                cr = value;
                ini.WriteValue("Settings", "Current_Revision", value);
            }
        }

        #endregion

        #region Installer Timeout

        private static int it = -1;

        public static int Installer_Timeout
        {
            get
            {
                if (it < 0) { it = ini.GetInt32("Settings", "Installer_Timeout", 120); }
                return it;
            }
            set
            {
                it = value;
                ini.WriteValue("Settings", "Installer_Timeout", value);
            }
        }

        #endregion

        #region Download Directory

        private static string dd;

        public static string Download_Directory
        {
            get
            {
                if (String.IsNullOrEmpty(dd))
                {
                    dd = filePathConvert(ini.GetString("Settings", "Download_Directory", "%CURRENT_DIRECTORY%/Downloads"));
                } 
                return dd;
            }
            set
            {
                dd = filePathConvert(value);
                ini.WriteValue("Settings", "Download_Directory", value);
            }
        }

        #endregion

        #region Auto Download

        private static int ad = -1;

        public static bool Auto_Download
        {
            get
            {
                if (ad < 0) { ad = ini.GetInt32("Settings", "Auto_Download", 0); }
                return Convert.ToBoolean(ad);
            }
            set
            {
                ad = Convert.ToInt32(value);
                ini.WriteValue("Settings", "Auto_Download", Convert.ToInt32(value));
            }
        }

        #endregion

        #region Show Updater Changes

        private static int suc = -1;

        public static bool Show_Updater_Changes
        {
            get
            {
                if (suc < 0) { suc = ini.GetInt32("Settings", "Show_Updater_Changes", 1); }
                return Convert.ToBoolean(suc);
            }
            set
            {
                suc = Convert.ToInt32(value);
                ini.WriteValue("Settings", "Show_Updater_Changes", Convert.ToInt32(value));
            }
        }

        #endregion

        #region CUpdater Log Path

        private static string culp;

        public static string CUpdater_Log_Path
        {
            get
            {
                if (String.IsNullOrEmpty(culp))
                {
                    culp = filePathConvert(ini.GetString("Settings", "CUpdater_Log_Path", "%CURRENT_DIRECTORY%/CUpdater.log"));
                }
                return culp;
            }
            set
            {
                culp = filePathConvert(value);
                ini.WriteValue("Settings", "CUpdater_Log_Path", value);
            }
        }

        #endregion

        #region Misc Functions

        private static string filePathConvert(string dd)
        {
            dd = dd.Replace("%CURRENT_DIR%", Application.StartupPath);
            dd = dd.Replace("%CURRENT_DIRECTORY%", Application.StartupPath);
            return dd;
        }

        #endregion
    }
}
